
clear all
cap log close
set more off
macro drop all
set mem 5000m
set processor 2
set maxvar 20000
set matsize 11000
cap log using "${hmrc_dir}\Log\iv_gdp_fy.log", text replace


timer on 1

/*foreach s in fy m2fy{
foreach x in export import{ */

foreach s in fy {

foreach x in export{
*** compute demand shocks as iv********************
qui use "${hmrc_datadir}\\`x'_reg_`s'", clear

drop if year<2004
drop if identifier==.

keep alpha3code
duplicates drop

*** take the cyclical component of GDP in destination countries
merge 1:m alpha3code using "${hmrc_datadir}\CBT\macr_IMF.dta" 
keep if _merge==3
drop _merge
egen ctry=group(alpha3code)
tsset ctry year
qui sum ctry
local max=r(max)
forv i=1/`max'{
tsfilter hp gdp_hp`i'=dgdp if ctry==`i', smooth (6.25)
}
gen gdp_hp=.
qui sum ctry
local max=r(max)
forv i=1/`max'{
replace gdp_hp=gdp_hp`i' if ctry==`i'
}

drop gdp_hp1-gdp_hp`max'
keep year alpha3code gdp_hp
qui save "${hmrc_datadir}\\`x'_gdp_hp_`s'1", replace

merge 1:m year alpha3code using "${hmrc_datadir}\\`x'_ctrygroup_reg_`s'"
keep if _merge==3
drop _merge

*** weigh the demand shock for each firm
bysort year identifier: egen totalexport_firm_year=total(svalue) 
bysort year ctry identifier: egen totalexport_firm_ctry_year=total(svalue) 
*share of firm i's trading in each country
gen share_ij=totalexport_firm_ctry_year/totalexport_firm_year
drop if svalue==0
*probability of profitable
keep identifier year alpha3code gdp_hp share_ij
duplicates drop
drop if identifier==.

qui egen identifier_ctry=group(identifier alpha3code)

xtset identifier_ctry year
bysort identifier_ctry: gen pi_ijt=gdp_hp*share_ij[_n-1]

bysort identifier year: egen pi_it=total(pi_ijt)

tab pi_ijt if pi_it==0
replace pi_it=. if pi_ijt==.

keep identifier year pi_it
duplicates drop
gen n=1 if pi_it==.
bysort identifier year: egen m=max(n)
drop if m==1
drop n m
save "${hmrc_datadir}\\`x'_`s'_profitshock1", replace
merge 1:m identifier year using "${hmrc_datadir}\\`x'_ctrygroup_reg_`s'"
drop _merge

*** use pi_it as an instrument of profitability

/*
tab profit_making
sum  pi_it if profit_making==1, d
sum  pi_it if profit_making==0, d

ttest pi_it, by(profit_making) unequal

xtreg profit_making pi_it, fe vce(robust)
xtreg trading_profit pi_it, fe vce(robust)
*/

qui keep if insample_mnc==1 
qui save "${hmrc_datadir}\\`x'_reg_mncs_`s'", replace

preserve
qui keep if insample_mnc==1 & type!="foreign_subs" /*UK mncs only*/
qui save "${hmrc_datadir}\\`x'_reg_ukmncs_`s'", replace
restore

qui keep if type=="foreign_subs"
qui save "${hmrc_datadir}\\`x'_reg_foreignmncs_`s'", replace

*******************************************************************************
**************************Regression Prep**************************************
*******************************************************************************

foreach t in ukmncs foreignmncs mncs {

qui use "${hmrc_datadir}\\`x'_reg_`t'_`s'1", replace
tab year
tab industry
keep if industry=="manufacturing"

*remove outliers based on unit price*
gen p_weight=svalue/netmass
gen p_unit=svalue/suppunit

qui sum p_weight, d
drop if p_weight>=r(p99) | p_weight<=r(p1)

qui sum p_unit, d
drop if (p_unit>=r(p99)&p_unit!=.) | p_unit<=r(p1) 

*regenerate log unit price and trade value
drop lnp_weight lnp_unit
gen lnp_weight=ln(p_weight)
gen lnp_unit=ln(p_unit)
gen lnsvalue=ln(svalue)

*sample clean*
qui gen drop1=turnover<svalue
tab drop1
drop if drop1==1
qui drop drop1

*more variables
gen r_wedge_sqr=related*tax_wedge_sqr

gen r_wedge_sqr_low=r_wedge_sqr*low_tax_country
gen r_wedge_sqr_high=r_wedge_sqr*high_tax_country

gen rlowwedge_sqr_pi=r_wedge_sqr*profit_making*low_tax_country
gen rhighwedge_sqr_pi=r_wedge_sqr*high_tax_country*profit_making

*iv for profitability
gen iv_r_wedge_sqr_pi=r_wedge_sqr*pi_it
gen iv_r_wedge_pi=related*pi_it
gen iv_tax_wedge_sqr=pi_it^2
gen iv_r_wedge_sqr=related*(pi_it^2)

gen lowwedge_pi=I_low_wedge*profit_making
gen highwedge_pi=I_high_wedge*profit_making

gen q1_wedge_pi=q1_wedge*profit_making
cap drop q1_r_wedge_pi
gen q1_r_wedge_pi = q1_r_wedge*profit_making

gen iv_lowwedge_pi=I_low_wedge*pi_it
gen iv_highwedge_pi=I_high_wedge*pi_it 

gen rlowwedge_pi=related*I_low_wedge*profit_making
gen rhighwedge_pi=related*I_high_wedge*profit_making
gen iv_rlowwedge_pi=related*I_low_wedge*pi_it
gen iv_rhighwedge_pi=related*I_high_wedge*pi_it

gen iv_rlowwedge_sqr_pi=r_wedge_sqr*low_tax_country*pi_it
gen iv_rhighwedge_sqr_pi=r_wedge_sqr*high_tax_country*pi_it

qui replace estimation_governance= estimation_governance/100000000

qui egen ctry_year=group(year alpha3code)
qui tab ctry_year, gen(year_ctry_d)
qui tab year, gen (year_d)

qui save "${hmrc_datadir}\\`x'_reg_final_`t'_`s'", replace
}


}

}
